import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demotest/views/favorites.dart';
import 'package:flutter_demotest/views/home.dart';
import 'package:flutter_demotest/views/messages.dart';
import 'package:flutter_demotest/views/profile.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class LayoutPage extends StatefulWidget {
  const LayoutPage({super.key});
  @override
  LayoutPageState createState() => LayoutPageState();
}

class LayoutPageState extends State<LayoutPage> {
  late DateTime _lastClickTime;
  int _selectedIndex = 0;

  final List<Widget> _pages = [
    HomePage(),
    FavoritesPage(),
    MessagesPage(),
    ProfilePage(),
  ];

  @override
  void initState() {
    super.initState();
    _lastClickTime = DateTime.now();
  }

  // 双击返回键退出提示
  Future<bool> _onWillPop() async {
    if (DateTime.now().difference(_lastClickTime) > Duration(seconds: 2)) {
      _lastClickTime = DateTime.now();
      SmartDialog.showToast(
        '再按一次返回键退出应用',
        alignment: Alignment.topCenter,
      );
      return false;
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        body: _pages[_selectedIndex],
        bottomNavigationBar: CurvedNavigationBar(
          backgroundColor: Colors.transparent,
          color: Colors.deepPurpleAccent.shade200.withOpacity(0.2),
          animationDuration: Duration(milliseconds: 400),
          onTap: (index) => setState(() => _selectedIndex = index),
          items: [
            Icon(Icons.home_outlined, color: Colors.white),
            Icon(Icons.favorite_outline, color: Colors.white),
            Icon(Icons.message_outlined, color: Colors.white),
            Icon(Icons.person_outlined, color: Colors.white),
          ],
        ),
      ),
    );
  }
}